/**
 * output file path into a file
 */
package cn.netjava.softkid.chapter07;
import cn.netjava.softkid.chapter02.DynamicArray;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @author Administrator
 *
 */
public class FilePath {
	DynamicArray da = new DynamicArray();

	/**
	 * add the absolute path and name of each file into dynamicArray
	 * @param name: file name
	 */
	public void getAllFileNames(String name,String sufix)
	{
		File f = new File(name);
		//list all files 
		File[] files = f.listFiles();
	
		if(files!=null)
		{
			for(int i=0;i<files.length;i++)
			{
				File temp = files[i];
				
				String fileName=temp.getName();
				
				if(temp.isFile()&&fileName.endsWith(sufix))
				{
					//add the absolute path and name of percent file into dynamicArray
					da.addObject(temp.getAbsolutePath()+fileName);
				}
				else if(temp.isDirectory())
				{
					//recursion call method getAllFileNames
					getAllFileNames(temp.getAbsolutePath(),sufix);				
				}
			}
		}

	
	}
	/**
	 * output the absolute path and name of each file
	 * @throws FileNotFoundException
	 */
	public void output() throws FileNotFoundException
	{
		da.output();
		
	}
	public void outputIntoFile() throws IOException
	{
		Object[] array=da.getDynamicArray();
		
		FileWriter fw = new FileWriter("D:\\PathName.txt");
		for(int i=0;i<da.getIndex();i++)
		{
			fw.write(i+":"+(String)array[i]+"\r\n");
		}
	}
	

}
